{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pymysql\n",
    "\n",
    "#打开数据库连接\n",
    "db = pymysql.connect(\n",
    "    host = '192.168.118.129',   #服务端地址\n",
    "    port = 3306,    #服务端端口\n",
    "    user = 'root',   #用户名\t\t\n",
    "    password = '123456',   #密码\n",
    "    database = 'cbgpt',   #要连接的数据库\n",
    ")\n",
    "\n",
    "#使用 cursor() 方法创建一个游标对象 cursor\n",
    "cursor = db.cursor(pymysql.cursors.DictCursor)\n",
    "\n",
    "#关闭数据库连接\n",
    "#db.close()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<pymysql.cursors.DictCursor object at 0x0000024C67407550>\n"
     ]
    }
   ],
   "source": [
    "print(cursor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "sql = \"select * from user\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cursor.execute(sql, None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'id': 1,\n",
       "  'username': '17879386605',\n",
       "  'password': 's1807931',\n",
       "  'nickname': 'esheeper',\n",
       "  'avatar': '2024-03/esheeper.png',\n",
       "  'createAt': datetime.datetime(2024, 3, 16, 11, 24, 11),\n",
       "  'updateAt': datetime.datetime(2024, 3, 16, 11, 24, 11)}]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cursor.fetchall()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "str = \"1787938661\"\n",
    "db.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
